package com.cj.service.impl;

import com.cj.dao.StudentDao;
import com.cj.po.ClassStudent;
import com.cj.po.PageInfo;
import com.cj.po.Student;
import com.cj.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户Service接口实现类
 */
@Service("studentService")
@Transactional
public class StudentServiceImpl implements StudentService {
    // 注入studentDao
    @Autowired
    private StudentDao studentDao;

    // 分页查询学生信息
    @Override
    public PageInfo<Student> findPageInfo(String s_name, Integer s_studentid, Integer s_classid,
                                          String s_classname, Integer pageIndex, Integer pageSize) {
        PageInfo<Student> pi = new PageInfo<>();
        pi.setPageIndex(pageIndex);
        pi.setPageSize(pageSize);
        // 获取总条数
        Integer totalCount = studentDao.totalCount(s_name, s_studentid, s_classid, s_classname);
        if (totalCount > 0) {
            pi.setTotalCount(totalCount);
            // 每一页显示学生信息数
            List<Student> studentList = studentDao.getStudentList(s_name, s_studentid, s_classid, s_classname,
                    (pi.getPageIndex() - 1) * pi.getPageSize(), pi.getPageSize());
            pi.setList(studentList);
        }
        return pi;
    }

    @Override
    public List<Student> getAll() {
        List<Student> studentList = studentDao.getAll();
        return studentList;
    }

    // 通过id删除学生信息
    @Override
    public int deleteStudent(Integer s_id) {
        return studentDao.deleteStudent(s_id);
    }

    // 添加学生信息
    @Override
    public int addStudent(Student student) {
        return studentDao.addStudent(student);
    }

    // 修改学生信息
    @Override
    public int updateStudent(Student student) {
        return studentDao.updateStudent(student);
    }

    @Override
    public Student findStudentById(Integer s_id) {
        Student s = studentDao.findStudentById(s_id);
        return s;
    }

    @Override
    public PageInfo<ClassStudent> findClassPageInfo(Integer c_classid, String c_classname, Integer pageIndex, Integer pageSize) {
        PageInfo<ClassStudent> pi = new PageInfo<>();
        pi.setPageIndex(pageIndex);
        pi.setPageSize(pageSize);
        // 获取总条数
        Integer totalCount = studentDao.classTotalCount(c_classid, c_classname);
        if (totalCount > 0) {
            pi.setTotalCount(totalCount);
            // 每一页显示学生信息数
            Map<String, Object> params = new HashMap<>();
            params.put("c_classid", c_classid);
            params.put("c_classname", c_classname);
            params.put("currentPage", (pi.getPageIndex() - 1) * pi.getPageSize());
            params.put("pageSize", pi.getPageSize());

            List<ClassStudent> classStudentList = studentDao.getClassStudentList(params);
            pi.setList(classStudentList);
        }
        return pi;
    }

    // 添加班级学生信息
    @Override
    public int addClassStudent(ClassStudent classStudent) {
        return studentDao.addClassStudent(classStudent);
    }

    // 通过id删除班级学生信息
    @Override
    public int deleteClassStudent(Integer s_id) {
        return studentDao.deleteClassStudent(s_id);
    }

    // 通过id查找班级学生信息
    @Override
    public ClassStudent findClassStudentById(Integer s_id) {
        return studentDao.findClassStudentById(s_id);
    }

    // 修改班级学生信息
    @Override
    public int updateClassStudent(ClassStudent classStudent) {
        return studentDao.updateClassStudent(classStudent);
    }

}
